﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PloobsEngine.SceneControl
{
    public class SunPostEffect : IPostEffect
    {
        #region IPostEffect Members
        private string flareTexture = "flare";
        private bool isInternalTexture = true;

        public bool IsInternalTexture
        {
            get { return isInternalTexture; }
            set { isInternalTexture = value; isInternalTexture = false; }
        }
               
        Effect effect = null;
        /// <summary>
        /// default = Color.White
        /// </summary>
        private Color sunColor = Color.White;

        public Color SunColor
        {
            get { return sunColor; }
            set { sunColor = value; }
        }
        /// <summary>
        /// default = 1
        /// </summary>
        private float sunIntensity = 1f;

        public float SunIntensity
        {
            get { return sunIntensity; }
            set { sunIntensity = value; }
        }
        /// <summary>
        /// default = 1500
        /// </summary>
        private float sunSunSize = 1500;

        public float SunSunSize
        {
            get { return sunSunSize; }
            set { sunSunSize = value; }
        }
        /// <summary>
        /// default = Vector3.Zero
        /// </summary>
        private Vector3 sunPosition = Vector3.Zero;

        public Vector3 SunPosition
        {
            get { return sunPosition; }
            set { sunPosition = value; }
        }

        Texture2D tex;


        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {
         
            effect.Parameters["Color"].SetValue(sunColor.ToVector3());
            effect.Parameters["lightIntensity"].SetValue(sunIntensity);
            effect.Parameters["SunSize"].SetValue(sunSunSize);
            effect.Parameters["lightPosition"].SetValue(sunPosition);
            effect.Parameters["flare"].SetValue(tex);

            effect.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
            effect.Parameters["VP"].SetValue(world.ActiveCamera.View * world.ActiveCamera.Projection);
            effect.Parameters["cameraPosition"].SetValue(world.ActiveCamera.Position);

            effect.Parameters["BackBufferTex"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
            effect.Parameters["Extra1"].SetValue(rHelper[PrincipalConstants.extra1RT]);
         
            rHelper.RenderTextureToFullScreenVertexPixel(effect); 
        }

        public void init(EngineStuff engine)
        {
            
            effect = EngineStuff.InternalContentManager.GetAsset<Effect>("Sun");
            if(isInternalTexture)
                tex = EngineStuff.InternalContentManager.GetAsset <Texture2D>("flare");
            else
                tex = EngineStuff.CustomContentManager.GetAsset<Texture2D>(flareTexture);
          
        }
       
        #endregion
    }
}
